package com.miraclengine.xml;

import java.util.ArrayList;
import java.util.List;

import com.miraclengine.utils.Debug;
import com.miraclengine.utils.GlobalStatic;

public class DataTable {
	private final static String TAG = DataTable.class.getSimpleName();
	
	List<String> columns = null;
	List<DataRow> rows = null;
	
	public DataTable() {
		rows = new ArrayList<DataRow>();
		columns = new ArrayList<String>();
	}
	
	//row
	public List<DataRow> getRows() {
		return this.rows;
	}
	
	public int getRowCount() {
		return this.rows.size();
	}
	
	public void addRow(DataRow dr) {
		this.rows.add(dr);
	}
	
	public DataRow newRow() {
		DataRow dr = new DataRow();
		return dr;
	}
	
	//column
	public List<String> getColumns() {
		return this.columns;
	}
	
	public boolean addColumn(String... col) {
		boolean retval = true;
		for(String column : col) {
			String c = DataTable.convc(column);
			if(columns.contains(c)) {
				retval = false;
			} else {
				columns.add(c);
			}
		}
		
		return retval;
	}
	
	public boolean removeColumns(String column) {
		String c = DataTable.convc(column);
		boolean retval = columns.remove(c);
		for(DataRow row : rows)  {
			row.getProperties().remove(c);
		}
		return retval;
	}
	
	//debug
	public void d() {
		if(GlobalStatic.DEBUG_MODE) {
			Debug.d(TAG, "- DataTable debug : (rows : " + this.getRowCount() + ")");
			for(DataRow dr : this.getRows()) {
				String str = "";
				for(String column : this.getColumns()) {
					Debug.i(TAG, "column : " + column);
					if(str.length() > 0) {
						str += ", ";
					}
					str += "[" + column + "] : " + dr.get(column);
				}
				Debug.d(TAG,  str);
			}
		}
	}
	
	//util
	public static String convc(String str) {
		return str.toUpperCase().trim();
	}
}
